﻿using System.Net.Sockets;
using System.Text;

namespace SocketClient
{
    internal class TCPClient
    {
        public void ConnectToServer(string serverIP, int serverPort)
        {
            TcpClient client = new TcpClient();
            client.Connect(serverIP, serverPort);

            NetworkStream stream = client.GetStream();

            // 发送消息到服务器
            string message = "Hello, server!";
            byte[] data = Encoding.ASCII.GetBytes(message);
            stream.Write(data, 0, data.Length);

            // 接收服务器的响应
            data = new byte[1024];
            int bytesRead = stream.Read(data, 0, data.Length);
            string response = Encoding.ASCII.GetString(data, 0, bytesRead);
            Console.WriteLine("服务器的响应: " + response);

            stream.Close();
            client.Close();
        }
    }
}